// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_MST_LOCATION
// * CreateDate    : 2011/8/16 10:16:40
// * Creater       : CodeSmith
// * LastChangeDate: 2011/8/16 10:16:40
// * LastChanger   : 2011/8/16 10:16:40
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Common;

namespace ESOL.Business.Mst
{
/// <summary>
    /// MstLocation
    /// </summary>
    public partial class MstLocationBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objMstLocationEntity">Entity</param>
        /// <returns>Entity</returns>
        public static MstLocationEntity GetMstLocation(MstLocationEntity objMstLocationEntity)
        {
            return DataAccess.SelectSingle<MstLocationEntity>(objMstLocationEntity);
        }

        public static List<MstLocationEntity> GetAllLocation(string warehouseCode)
        {
            string sql = @"SELECT L.* 
                            FROM MST_LOCATION L
                            INNER JOIN MST_WAREHOUSE W ON W.WAREHOUSE_CODE = L.WAREHOUSE_CODE
                            WHERE W.DELETE_FLAG <> 1 ";
            if (string.IsNullOrEmpty(warehouseCode) == false)
            {
                sql += " AND W.WAREHOUSE_CODE = '" + warehouseCode + "'";
            }
            sql += " ORDER BY LOCATION_NAME";
            return DataAccess.Select<MstLocationEntity>(sql);
        }

        public static DataTable GetLocationByWarehouseCode(string WarehouseCode)
        {
            string sql = "select * from mst_location where WAREHOUSE_CODE='" + WarehouseCode + "' and ISUSERED=0";

            return DataAccess.SelectDataSet(sql).Tables[0];

        }

        public static DataTable GetByCondition_Code(string condition, string orderBy)
        {
            string sql = @"SELECT * FROM DBO.MST_LOCATION A LEFT JOIN DBO.MST_WAREHOUSE B ON A.WAREHOUSE_CODE=B.WAREHOUSE_CODE WHERE 1=1 ";
            if (string.IsNullOrEmpty(condition) == false)
            {
                sql += condition;
            }
            if (string.IsNullOrEmpty(orderBy) == false)
            {
                sql += " ORDER BY " + orderBy;
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
        #endregion

        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objMstLocationEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertMstLocation(MstLocationEntity objMstLocationEntity)
        {
            return DataAccess.Insert(objMstLocationEntity);
        }

        public static bool InsertMstLocation(List<MstLocationEntity> objMstLocationEntityList)
        {
            return DataAccess.Insert(objMstLocationEntityList);
        }

        public static bool InsertByBatch(string sInvCode, string sCodeTitle, int Qty)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try 
                {
                    List<MstLocationEntity> list = new List<MstLocationEntity>();
                    for (int i = 0; i < Qty; i++)
                    {
                        MstLocationEntity entity = new MstLocationEntity();
                        entity.WarehouseCode = sInvCode;
                        entity.LocationId = Guid.NewGuid().ToString();
                        entity.LocationCode = sCodeTitle + (i+1).ToString().PadLeft(3,'0');
                        entity.Isusered = 0;
                        list.Add(entity);
                    }
                    DataAccess.Insert(list, broker);

                    broker.Commit();
                    return true;
                }
                catch
                {
                    broker.RollBack();
                    return false;
                }
            }
        }
        #endregion

        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objMstLocationEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateMstLocation(MstLocationEntity objMstLocationEntity)
        {
            return DataAccess.Update(objMstLocationEntity);
        }

        public static bool UpdateMstLocation(List<MstLocationEntity> objMstLocationEntityList)
        {
            return DataAccess.Update<MstLocationEntity>(objMstLocationEntityList);
        }
        #endregion

        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objMstLocationEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteMstLocation(MstLocationEntity objMstLocationEntity)
        {
            return DataAccess.Delete(objMstLocationEntity);
        }

        public static bool DeleteMstLocation(List<MstLocationEntity> objMstLocationEntityList)
        {
            return DataAccess.Delete<MstLocationEntity>(objMstLocationEntityList);
        }
        #endregion

        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetMstLocationAll()
        {
            return DataAccess.SelectAllDataSet<MstLocationEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<MstLocationEntity> GetMstLocationAllList()
        {
            return DataAccess.SelectAll<MstLocationEntity>();
        }
        #endregion

        #region Functions
        public static MstLocationEntity GetMstLocationByID(string LocationID)
        {
            if (string.IsNullOrEmpty(LocationID))
                return null;
            string sql = @"SELECT * FROM MST_LOCATION A WHERE 1=1 
                AND A.LOCATION_CODE='" + LocationID + "' ORDER BY A.LOCATION_CODE";
            MstLocationEntity mMstLocation = new MstLocationEntity();
            mMstLocation.LocationId = LocationID;
            return DataAccess.SelectSingle<MstLocationEntity>(mMstLocation);
        }

        public static DataTable GetWarehouseLocationByLocationId(string LocationID)
        {
            string sql = "select * from dbo.MST_LOCATION A left join dbo.MST_WAREHOUSE B ON A.WAREHOUSE_CODE=B.WAREHOUSE_CODE WHERE A.LOCATION_ID='" + LocationID + "'";

            return DataAccess.SelectDataSet(sql).Tables[0];

        }
        #endregion
    }
	
	
}	

